package extras.mak001;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;

public class SigUpdater {

	private static boolean isDone = false;
	private static boolean finished = false;
	private static String sname = "";

	public static int loop(String[] scriptInfo) {
		StringBuilder result = new StringBuilder();
		for (int i = 0; i < scriptInfo.length; i++) {
			if (i > 0)
				result.append("and" + scriptInfo[i]);
			else
				result.append(scriptInfo[i]);
		}
		updateSignature(result.toString().replace("=", "equals"));
		return 0;
	}

	public static void updateSignature(String scriptStats) {
		try {
			URL url;
			URLConnection urlConn;
			url = new URL("http://mak001.x10.mx/dynamicsig/updater.php");
			urlConn = url.openConnection();
			urlConn.setRequestProperty("User-Agent", "| supersecret |");
			urlConn.setDoOutput(true);
			urlConn.setDoInput(true);
			urlConn.setUseCaches(false);
			urlConn.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			String content = "script=" + sname + "&ss=" + scriptStats; // TODO
			DataOutputStream wr = new DataOutputStream(urlConn
					.getOutputStream());
			wr.writeBytes(content);
			String line;
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));
			while ((line = rd.readLine()) != null) {
			//	Logger.getLogger("sig updater").info(
			//			line.replace("eol", System
			//					.getProperty("line.separator")));
			}

			wr.flush();
			wr.close();
			rd.close();
			if (isDone == true) {
				finished = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void setIsDone(boolean b) {
		isDone = b;
	}

	public static boolean isNotDone() {
		return finished;
	}

	public static void setScript(String script) {
		sname = script;
	}

}
